--##[These are your variables]##

bk = Image.createEmpty(480, 272)
logbg = Image.createEmpty(480, 272)
log = 0
logd = 0
interface = Image.createEmpty(480, 272)
lacti = 0
startx = 0
starty = 0
callpicker = 0
endx = 0
endy = 0
cury = 20
c1 = 252
c2 = 252
c3 = 252
al = 255
x = 200
y = 25
xr = 0
yr = 0
control = 1
osd = 0
dposy = 0
posy = 0
posx = 0
num = 0
tlvl = 255
pad = Controls.read()

--##[Begin your game with this code]##
while true do
---- ########  need to be here for live alpha-ing ########
r =Color.new(255,0,0,tlvl)
g =Color.new(0,255,0,tlvl)
g1 =Color.new(0,200,0,tlvl)
b =Color.new(0,0,255,tlvl)
bl =Color.new(0,0,0)
o =Color.new(255,255,0,tlvl)
w =Color.new(255,255,255,tlvl)
  pad = Controls.read()
---- ########  activate dev overlay ########
if pad:select() and oldpad:select() ~= pad:select() then
  osd = osd + 1
    if osd == 2 then
      interface:clear()
      osd = 0
    end
end

[YOUR CODE HERE]
[YOUR CODE HERE]
[YOUR CODE HERE]
[YOUR CODE HERE]
[YOUR CODE HERE]
[YOUR CODE HERE]
  if osd ~= 1 then
[PUT SCREEN FLIPPING AND ALL CONTROL FUNCTIONS IN HERE]
  end
---- ########  Dev App ########
if osd == 1 then
  m =Color.new(c1,c2,c3,al)  
  tlvl = 255
  interface:fillRect(0, 0, 480, 14,g1)
  interface:fillRect(4, 0, 480, 8,g)
  interface:fillRect(2, 2, 86, 10,bl)
  interface:print(4,3,"X:".. x,r)
  interface:print(49,3,"Y:".. y,r)
  interface:print(99,3,"AX:".. xr,r)
  interface:print(155,3,"AY:".. yr,r)
  interface:print(215,3,"StartX:".. startx,r)
  interface:print(305,3,"StartY:".. starty,r)
  interface:print(215,16,"EndX:".. endx,r)
  interface:print(305,16,"EndY:".. endy,r)
  screen:blit(0,0,interface)  
  screen:blit(0,0,bk)  
  screen:fillRect(x, y, 1, 1,r)
  screen:drawLine(x, y+3, x,y+6,r)
  screen:drawLine(x+3, y, x+6, y,r)
  screen:drawLine(x, y-3, x, y-6,r)
  screen:drawLine(x-3, y, x-6, y,r)
  if cury == 260 then
    cury = 20
    logbg:clear()
  end
  if num == 24 then
    num = 0
  end
  if log == 1 then
    screen:blit(0,0,logbg)
  end
  if lacti == 3 then
      endx = x
      endy = y
      num = num + 1
      cury = cury + 10
      logbg:print(0,cury,num..")Startx:"..startx.." Starty:"..starty.." Endx:"..endx.." Endy:".. endy..  " R:".. c1..  " G:".. c2..  " B:".. c3,bl)      
      bk:drawLine(startx, starty, endx, endy, m)
      lacti = 0
    end              
  if lacti == 2 and tool == 0 then
      screen:drawLine(startx, starty, x, y, m)
    end              
---- ########  call color picker ########
if callpicker == 1 then
  interface:fillRect(0,205,100,55,w)
        if dposy == 1 then
      screen:print(0,220,"oR:".. c1,r)
        else
           screen:print(0,220,"R:".. c1,r)
        end
  if dposy == 2 then
      screen:print(0,230,"oG:".. c2,g)
        else
           screen:print(0,230,"G:".. c2,g)
  end
  if dposy == 3 then
      screen:print(0,240,"oB:".. c3,b)
        else
           screen:print(0,240,"B:".. c3,b)
  end
      screen:print(0,210,"Color Picker",g)
      screen:fillRect(5,250,30,5,m)
---- ########  RGB ########
    if pad:left() and dposy == 1 then
      c1 = c1 - 2
    end              
    if pad:left() and dposy == 2 then
      c2 = c2 - 2
    end              
    if pad:left() and dposy == 3 then
      c3 = c3 - 2
    end              
    if pad:right() and dposy == 1 then
      c1 = c1 + 2
    end              
    if pad:right() and dposy == 2 then
      c2 = c2 + 2
    end              
    if pad:right() and dposy == 3 then
      c3 = c3 + 2
    end              
-- ######## menu navigation ########
    if pad:up() and oldpad:up() ~= pad:up() then
      dposy = dposy - 1
    end              
    if pad:down() and oldpad:down() ~= pad:down() then
      dposy = dposy + 1
    end              
    if dposy == 0 then
      dposy = 3
    end              
    if dposy == 4 then
      dposy = 1
    end              
-- ######## colors ########
    if c1 == 2 then
      c1 = 252
    end              
    if c2 == 2 then
      c2 = 252
    end              
    if c3 == 2 then
      c3 = 252
    end              
    if c1 == 254 then
      c1 = 4
    end              
    if c2 == 254 then
      c2 = 4
    end              
    if c3 == 254 then
      c3 = 4
    end
end
-- ######## log ########
    if pad:l() and oldpad:l() ~= pad:l() and callpicker ~= 1 then
      if log == 1 then
        interface:clear()
        log = 0
        control = 1
          else
            interface:fillRect(0,0,480,272,w)
            log = 1
            control = 0
       end
     end     
    if pad:r() and oldpad:r() ~= pad:r() and log ~= 1 then
      if callpicker == 0 then   
        control = 0
        callpicker = 1
          else
            callpicker = 0
            control = 1
      end
    end                            
--  speed 1
  if control == 1 then
    if pad:analogX() > -40 then
      x = x + 1
    end              
    if pad:analogX() < 40 then
      x = x - 1
    end
    if pad:analogY() > -40 then
      y = y + 1
    end
    if pad:analogY() < 40 then
      y = y - 1
    end
--  speed 5
    if pad:analogX() > -120 then
      x = x + 5
    end              
    if pad:analogX() < 120 then
      x = x - 5
    end
    if pad:analogY() > -120 then
      y = y + 5
    end
    if pad:analogY() < 120 then
      y = y - 5
    end
    if x >= 480 then
      x = 480
    end
    if x <= 0 then
      x = 0
    end
    if y >= 272 then
      y = 272
    end
    if y <= 0 then
      y = 0
    end
    yr = pad:analogY()
    xr = pad:analogX()
    if pad:up() and oldpad:up() ~= pad:up() then
      y = y - 1
    end              
    if pad:down() and oldpad:down() ~= pad:down() then
      y = y + 1
    end  
    if pad:left() and oldpad:left() ~= pad:left() then
      x = x - 1
    end              
    if pad:right() and oldpad:right() ~= pad:right() then
      x = x + 1
    end 
-- ######## Line Drawing ########
    if pad:cross() and oldpad:cross() ~= pad:cross() then
      lacti = lacti + 1
    end              
    if pad:square() and oldpad:square() ~= pad:square() then
      lacti = 0
      screen:clear()
    end              
    if pad:triangle() and oldpad:triangle() ~= pad:triangle() then
      bk:clear()
    end              
    if lacti == 1 then
      startx = x
      starty = y
      lacti = 2
    end
end
  screen.flip()
if callpicker ~= 1 and log ~= 1 then
  interface:clear()
end
 end
  oldpad = pad
--####End of Entire LUA Program##--
end